package com.dankegongyu.lib.common.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.app.NotificationManagerCompat;

/**
 * https://github.com/Blankj/AndroidUtilCode/blob/master/lib/utilcode/src/main/java/com/blankj/utilcode/util/NotificationUtils.java
 *
 * @author wupuquan
 * @date 2020/7/10 12:43
 */
public class DKNotificationUtils {

    private DKNotificationUtils() {
    }

    /**
     * 检测是否有通知权限
     */
    public static boolean areNotificationsEnabled(Context context) {
        // 对于api19以下的版本该方法返回的值为true;
        NotificationManagerCompat manager = NotificationManagerCompat.from(context);
        return manager.areNotificationsEnabled();
    }

    /**
     * 打开通知权限设置页面
     */
    public static void openNotificationSetting(Context context) {
        try {
            // 根据isOpened结果，判断是否需要提醒用户跳转AppInfo页面，去打开App通知权限
            Intent intent = new Intent();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, context.getApplicationInfo().uid);
            }
//            else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                // 实测发现vivo X9 7.1.2跳转的页面不是对应开启通知页面，舍弃这种方案
//                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
//                intent.putExtra("app_package", context.getPackageName());
//                intent.putExtra("app_uid", context.getApplicationInfo().uid);
//            }
            else {
                // 下面这种方案是直接跳转到当前应用的设置界面
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.fromParts("package", context.getPackageName(), null));
            }
            context.startActivity(intent);
        } catch (Exception e) {
//            e.printStackTrace();
        }
    }
}
